unit oSerializer;

interface
uses Xmlintf, XMLDoc, SysUtils, XMLDOm, Classes;
type

  // class for storing and retrieving data from xml
   TSerializer = class
   public
    // saveValueToXML - inserts paNode element with name tag and paValue
    // loadFromXML - gets paValue from paNode element with name tag
    class procedure saveValueToXML(var paNode : IXmlnode; paTag : string; paValue : integer); overload;
    class procedure loadValueFromXML(paNode : IXmlnode; paTag : string; var paValue : integer);overload;
    class procedure saveValueToXML(var paNode : IXmlnode; paTag : string; paValue : double); overload;
    class procedure loadValueFromXML(paNode : IXmlnode; paTag : string; var paValue : double);overload;
    class procedure loadValueFromXML(paNode : IXmlnode; var paValue : string);overload;
    class procedure saveValueToXML(var paNode : IXmlnode; paTag : string; paValue : boolean); overload;
    class procedure loadValueFromXML(paNode : IXmlnode; paTag : string; var paValue : boolean);overload;
    class procedure saveValueToXML(var paNode : IXmlnode; paTag : string; paValue : real); overload;
    class procedure saveValueToXML(var paNode : IXmlnode; paTag : string; paValue : string); overload;
    class procedure loadValueFromXML(paNode : IXmlnode; paTag : string; var paValue : real);overload;
    class procedure loadValueFromXML(paNode : IXmlnode; paTag : string; var paValue : string);overload;
    class procedure loadMapFromXML(paNode : IXMLNode; var paKey, paValue : string); overload;
    // creats xml file on path paPath with paNode as root node
    class procedure SaveToXML(paPath : string; paNode : IXMLNode);
    class procedure saveMapToXML(var paNode : IXMLNode; paTag : string; paKey, paValue : string); overload;
    // returns the content of xml file as Tstringlist
    class function getStrings(paPath : string) : TStringList;
    class function prepareXMLDoc : TXMLDocument;
   end;

implementation

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; paTag: string;
  var paValue: string);
var
  node : IXMLNode;
begin
  node := paNode.ChildNodes[paTag];
  if node.Text <> '' then
   paValue := node.Text
  else
   paValue := '';
end;

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; paTag: string;
  var paValue: double);
var
  node : IXMLNode;
begin
  node := paNode.ChildNodes[paTag];
  if node.Text <> '' then
   paValue := StrToFloat(node.Text)
  else
   paValue := 0;
end;

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; var paValue: string);
begin

  if paNode.Text <> '' then
   paValue := paNode.Text
  else
   paValue := '';
end;

class procedure TSerializer.saveValueToXML(var paNode: IXmlnode; paTag,
  paValue: string);
var
  node : IXMLNode;
begin
  node := paNode.AddChild(paTag);
  node.Text := paValue;

end;


class procedure TSerializer.saveValueToXML(Var paNode: IXmlnode; paTag: string; paValue: real);
var
  node : IXMLNode;
begin
  node := paNode.AddChild(paTag);
  node.Text := FloatToStr(paValue);
end;

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; paTag: string;
  var paValue: integer);
var
  nv : IXMLnode;
begin
    nv := panode.ChildNodes[paTag];
    if nv.Text <> '' then
      paValue := StrToInt(nv.Text)
    else
      paValue := 0;
end;

class procedure TSerializer.saveValueToXML(var paNode: IXmlnode; paTag: string;
  paValue: integer);
var
  node : IXMLNode;
begin
  node := paNode.AddChild(paTag);
  node.Text := IntToStr(paValue);

end;

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; paTag: string; Var paValue: real);
var
  nv : IXMLnode;
begin
    nv := panode.ChildNodes[paTag];
    if nv.Text <> '' then
      paValue := StrToFloat(nv.Text)
    else
      paValue := 0;
end;
class procedure TSerializer.SaveToXML(paPath: string; paNode: IXMLNode);
var
  xml : TXMLDocument;
begin
  xml := TXMLDocument.Create(nil);
  xml.DomVendor := DOMVendors.Vendors[0];
  xml.Options := xml.Options + [doNodeAutoIndent];
  xml.Active := true;
  xml.DocumentElement := paNode;
  xml.SaveToFile(paPath);
  xml.Destroy;
end;

class procedure TSerializer.saveValueToXML(var paNode: IXmlnode; paTag: string;
  paValue: double);
var
  node : IXMLNode;
begin
  node := paNode.AddChild(paTag);
  node.Text := FloatToStr(paValue);
end;

class procedure TSerializer.loadMapFromXML(paNode: IXMLNode;
  var paKey, paValue: string);
begin
  loadValueFromXML(paNode, 'key', paKey);
  loadValueFromXML(paNode, 'value', paValue);
end;

class procedure TSerializer.saveMapToXML(var paNode: IXMLNode; paTag, paKey,
  paValue: string);
var
  n : IXMLNode;
begin
  n := paNode.AddChild(paTag);
  saveValueToXML(n, 'key', paKey);
  saveValueToXML(n, 'value', paValue);
end;

class function TSerializer.getStrings(paPath : string): TStringList;
var
  f : text;
  strings : TStringList;
  s : string;
begin
  strings := TStringList.Create;
  try
    AssignFile(f, paPath);
    if FileExists(paPath) then
     begin
      reset(f);
      while not eof(f) do
      begin
       readln(f, s);
       strings.Add(s);
      end;
     end;
  finally
    if FileExists(paPath) then
      CloseFile(f)
  end;

  Result := strings;
end;

class function TSerializer.prepareXMLDoc: TXMLDocument;
var
  xml : TXMLDocument;
begin
  xml := TXMLDocument.Create(nil);
  xml.DomVendor := DOMVendors.Vendors[0];
  xml.Options := xml.Options + [doNodeAutoIndent];
  Result := xml;
end;

class procedure TSerializer.loadValueFromXML(paNode: IXmlnode; paTag: string;
  var paValue: boolean);
var
  nv : IXMLnode;
begin
    nv := panode.ChildNodes[paTag];
    if nv.Text <> '' then
      paValue := StrToBool(nv.Text)
    else
      paValue := false;
end;

class procedure TSerializer.saveValueToXML(var paNode: IXmlnode;
  paTag: string; paValue: boolean);
var
  node : IXMLNode;
begin
  node := paNode.AddChild(paTag);
  node.Text := BoolToStr(paValue);
end;

end.

